package com.itheima.service.impl;

import cn.itheima.health.service.UserService;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByUserName(String username) {
        //  select  * from  t_user  where username = ?
      User user =   userMapper.findUserByUserName(username);
      //   user---Roles    users_roles   roles
        Set<Role> userRoles = userMapper.findRolesByUid(user.getId());
        if(userRoles!=null&&userRoles.size()!=0){
            for (Role role : userRoles) {
                //   通过角色id  查询 对应的权限
                Set<Permission>  permissionsByRoleIds= userMapper.findPermissionsByRoleId(role.getId());
                role.setPermissions(permissionsByRoleIds);
            }
        }
        user.setRoles(userRoles);
        return user;
    }
}
